﻿using Api.Core.IRepository.Role;
using Api.Core.IRepository.User;
using Api.Core.IServices;
using Api.Core.IServices.User;
using Api.Core.Model;
using Api.Core.Model.Entity;
using Api.Core.Model.Table;
using Api.Core.Model.ViewModel;
using SqlSugar;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Threading.Tasks;

namespace Api.Core.Services.User
{
    /// <summary>
    /// 
    /// </summary>
    public class UserService : BaseService<UserTable>, IUserService
    {
        private IUserRepository userRepository;

        private IGroupRoleRespository groupRoleRespository;

        private IGroupRespository groupRepository;

        public UserService(IUserRepository _userRepository,IGroupRoleRespository _groupRoleRespository, IGroupRespository _groupRepository)
        {
            base.BaseDal = _userRepository;
            userRepository = _userRepository;
            groupRoleRespository = _groupRoleRespository;
            groupRepository = _groupRepository;
        }



        /// <summary>
        /// 根据UserId获取用户组信息
        /// </summary>
        /// <param name="model"></param>
        /// <returns></returns>
        public async Task<List<UserEntity>> GetUserGroup(UserViewModel model)
        {
           var result =  await userRepository.GetUserGroup(model);
            return result;
        }

        #region 新增

        /// <summary>
        /// 
        /// </summary>
        /// <param name="model"></param>
        /// <returns></returns>
        public async Task<BaseResponse<bool>> AddUserWithGroup(UserEntity model)
        {
            var noGroup = await groupRepository.QueryOne(x=> !model.GroupIds.Contains(x.GroupId));
            if (noGroup != null && noGroup.Id > 0)
            {
                return Result(false,StatusCode.Fail,"组不存在，请重新选择");
            }
            model.UserId = Guid.NewGuid().ToString();
            model.Status = 1;
            model.CreateTime = DateTime.Now;
            model.ModifityTime = DateTime.Now;
            var result = await userRepository.AddUserWithGroup(model);
          
            return Result(result);
        }
        #endregion

        //public async Task<List<UserRoleInfoEntity>> GetUserRoles(UserViewModel model)
        //{ 
        //}

        ///// <summary>
        ///// 
        ///// </summary>
        ///// <param name="model"></param>
        ///// <returns></returns>
        //public async Task<PageModel<Model.Entity.UserInfoEntity>> GetUser(UserViewModel model)
        //{
        //    string where = "";
        //    if (!string.IsNullOrWhiteSpace(model.UserId))
        //    {
        //        where += " UserId = '" + model.UserId + "' ";
        //    }
        //    if (!string.IsNullOrWhiteSpace(model.UserName))
        //    {
        //        where += " UserName Like '" + model.UserName + "%' ";
        //    }

        //    var result = await base.QueryMuch<UserTable, UserGroupTable, GroupRoleTable, UserInfoEntity>(1, 10, "", where,
        //        (u, ug, g) => new object[] {
        //            JoinType.Left, u.UserId ==ug.UserId,
        //            JoinType.Left ,ug.GroupId ==g.GroupId
        //        }, (u, ug, g) => new UserInfoEntity()
        //        {
        //        });
        //    //var result = await base.QueryMuch<UserTable, RoleTable, UserRoleTable, UserInfoEntity>(1, 10, "", where, (a, b, c) => new object[] {
        //    //    JoinType.Left,a.UserId ==c.UserId,
        //    //    JoinType.Inner,c.RoleId ==b.RoleId
        //    //}, (a, b, c) => new UserInfoEntity()
        //    //{
        //    //    RoleId = b.RoleId,
        //    //    RoleName = b.RoleName,
        //    //    Status = a.Status,
        //    //    UserId = a.UserId,
        //    //    UserName = a.UserName
        //    //});
        //    return result;
        //}

    }

}
